package cn.zj.admin.controller;

import cn.zj.admin.attr.ModelAttr;
import cn.zj.admin.util.BeanUtils;
import com.zj.utils.StringUtil;
import com.zj.utils.eo.Regex;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * @author xi.yang
 * @create 2020-10-16 18:23
 **/
@Controller
@RequestMapping("view")
public class ZjComController {

    @GetMapping("{system}/{module}/{func}")
    public String commonView(HttpServletRequest request, @PathVariable("system") String system, @PathVariable("module") String module, @PathVariable("func") String func, ModelMap modelMap) {
        addAttrs(request, modelMap);
        return StringUtil.appendStr(system, "/", module, "/", func);
    }

    @GetMapping("{system}/{module}")
    public String commonView(HttpServletRequest request, @PathVariable("system") String system, @PathVariable("module") String module, ModelMap modelMap) {
        addAttrs(request, modelMap);
        return StringUtil.appendStr(system, "/", module);
    }

    private void addAttrs(HttpServletRequest request, ModelMap modelMap) {
        String zjModelAttrs = request.getParameter("zjModelAttrs");
        String[] attrs = StringUtil.splitByRegex(zjModelAttrs, Regex.COMMA);
        for (String attr : attrs) {
            ModelAttr modelAttr = BeanUtils.getBean(attr, ModelAttr.class);
            if (modelAttr != null) {
                modelAttr.setAttr(request, modelMap);
            }
        }
    }
}
